﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;

namespace WebLayer
{
    public partial class BookingPageOne : System.Web.UI.Page
    {

        string dbConnectionString = "Data Source=balder.ucn.dk;Initial Catalog=dmaa0913_5;User ID=dmaa0913_5;Password=MaaGodt";
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //clear all textboxes

            ClearInputs(Page.Controls);
            
        }

        void ClearInputs(ControlCollection ctrls)
        {
            foreach (Control ctrl in ctrls)
            {
                if (ctrl is TextBox)
                    ((TextBox)ctrl).Text = string.Empty;
                ClearInputs(ctrl.Controls);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(dbConnectionString);
            
            try
            {
                conn.Open();
                string inse = "insert into [customer] (ssn, creditNu, name, street, house, zip, phone, email) value (@ssn, @creditNo, name, stree, house, sip, phone, email)";
                SqlCommand cmd = new SqlCommand(inse, conn);
                {
                    cmd.Parameters.AddWithValue("@ssn", ssntxt.Text);
                    cmd.Parameters.AddWithValue("@creditNo", credittxt.Text);
                    cmd.Parameters.AddWithValue("@name", firstnametxt.Text);
                    cmd.Parameters.AddWithValue("@street", roadtxt.Text);
                    cmd.Parameters.AddWithValue("@house", housetxt.Text);
                    cmd.Parameters.AddWithValue("@zipCode", ziptxt.Text);
                    cmd.Parameters.AddWithValue("@phone", phonetxt.Text);
                    cmd.Parameters.AddWithValue("@email", mailtxt.Text);

                    try
                    {
                        cmd.ExecuteNonQuery();
                        Response.Redirect("UserLogin.aspx");
                    }

                    catch
                    {
                        Response.Write("Somthing went wrong ..... Please try again");
                    }

                    finally
                    {
                        conn.Close();
                    }
                }


                
            }
            finally
            {
                if (conn != null)
                {
                    //cleanup connection i.e close 
                }
            }
            
        }
    }
}